package com.keqiang.chart.impl.pie

import android.graphics.Canvas
import android.graphics.Color
import androidx.annotation.FloatRange
import androidx.annotation.Px
import com.keqiang.chart.base.BaseChart
import com.keqiang.chart.base.BaseChartView
import com.keqiang.chart.impl.pie.render.IDataRender
import com.keqiang.chart.impl.pie.render.ILabelRender
import com.keqiang.chart.utils.px

/**
 * 饼状图基类
 *
 * @param D 数据类型
 * @author Created by wanggaowan on 2022/8/1 16:42
 */
abstract class BasePieChart<D>(view: BaseChartView<*>) : BaseChart<D>(view) {

    /**
     * 饼状图半径
     */
    @setparam:Px
    @setparam:FloatRange(from = 0.0)
    open var radius: Float = 200f.px
        set(value) {
            if (value < 0) {
                return
            }
            field = value
        }

    /**
     * 内圆半径，== 0则绘制扇形，> 0则绘制圆环
     */
    @setparam:Px
    @setparam:FloatRange(from = 0.0)
    var innerRadius: Float = 0f
        set(value) {
            field = if (value < 0) {
                0f
            } else {
                value
            }
        }

    /**
     * 每个扇形之间的间距
     */
    @Px
    @setparam:FloatRange(from = 0.0)
    var sliceSpace: Float = 0f
        set(value) {
            field = if (value < 0) {
                0f
            } else {
                value
            }
        }

    /**
     * 底图颜色，未设置任何饼状图数据时绘制的底图。
     */
    open var baseMapColor: Int = Color.TRANSPARENT

    /**
     * 起始绘制的角度
     */
    open var drawStartAngle: Float = 0f

    /**
     * 数据绘制器
     */
    abstract val dataRender: IDataRender

    /**
     * 标签绘制器
     */
    abstract val labelRender: ILabelRender?

    /**
     * 标签绘制位置
     */
    open var labelPosition: LabelPosition = LabelPosition.inside()

    abstract override val touchHandler: BaseTouchHandler<out BasePieChart<*>, *>?

    override fun onDraw(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float) {
        if (radius < 0 || innerRadius >= radius) {
            return
        }

        val startAngle = drawStartAngle + (touchHandler?.rotation ?: 0f)
        dataRender.onDraw(canvas, startAngle)
        labelRender?.onDraw(canvas)
    }

    /**
     * 获取中心点坐标
     */
    open fun getCenter(): Pair<Float, Float> {
        val width = view.width - view.paddingStart - view.paddingEnd
        val height = view.height - view.paddingTop - view.paddingBottom
        val centerX = width / 2f + view.paddingStart
        val centerY = height / 2f + view.paddingTop
        return Pair(centerX, centerY)
    }
}

/**
 * 3D饼状图基类
 *
 * @param D 数据类型
 * @author Created by wanggaowan on 2022/8/15 16:09
 */
abstract class BasePieChart3D<D>(view: BaseChartView<*>) : BasePieChart<D>(view) {

    /**
     * 3D饼状图高度,此高度为[flipAngle]为90°状态下的高度
     */
    @Px
    @setparam:FloatRange(from = 0.0)
    open var height: Float = 30f.px
        set(value) {
            field = if (value < 0) {
                0f
            } else {
                value
            }
        }

    /**
     * 3D饼状图翻转角度，0°则和二维饼状图一致，90°则只能看到一个二维矩形.允许取值范围为0°~90°
     */
    @setparam:FloatRange(from = 0.0, to = 90.0)
    open var flipAngle: Float = 30f
        set(value) {
            field = if (value < 0) {
                0f
            } else if (value > 90) {
                90f
            } else {
                value
            }
        }
}